package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.UserFavoriteActivity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户收藏活动Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface UserFavoriteActivityMapper extends BaseMapper<UserFavoriteActivity> {

    /**
     * 检查用户是否收藏了某个活动
     */
    @Select("SELECT * FROM user_favorite_activity " +
            "WHERE userid = #{userid} AND activityid = #{activityid} " +
            "ORDER BY favoriteid DESC LIMIT 1")
    UserFavoriteActivity checkFavorite(@Param("userid") Integer userid,
                                       @Param("activityid") Integer activityid);

    /**
     * 更新收藏状态
     */
    @Update("UPDATE user_favorite_activity SET state = #{state}, createdAt = NOW() " +
            "WHERE userid = #{userid} AND activityid = #{activityid}")
    int updateFavoriteState(@Param("userid") Integer userid,
                           @Param("activityid") Integer activityid,
                           @Param("state") Integer state);

    /**
     * 插入新的收藏记录
     */
    @Update("INSERT INTO user_favorite_activity (userid, activityid, state, createdAt) " +
            "VALUES (#{userid}, #{activityid}, #{state}, NOW())")
    int insertFavorite(@Param("userid") Integer userid,
                      @Param("activityid") Integer activityid,
                      @Param("state") Integer state);
} 